package com.wx.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.vo.order.OrderRepVo;
import com.wx.mall.order.entity.Order;
import com.wx.mall.order.mapper.OrderMapper;
import com.wx.mall.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.order.vo.OrderListReqVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-04-08
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Override
    public List<Order> selectByStatus() {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status",1).eq("is_delete",1);
        List<Order> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Order> find(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete",1);
        List<Order> list=this.list(queryWrapper);
        return list;
    };

    @Override
    public List<Order> pageOrder(Integer pageNum, Integer pageSize, int sort) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        0为创建时间 1为支付方式 2订单来源  3订单状态
        if(sort==0){
            queryWrapper.orderByAsc("create_time");
        };
        if (sort==1){
            queryWrapper.orderByAsc("pay_type");
        };
        if(sort==2){
            queryWrapper.orderByAsc("source_type");
        };
        if (sort==3){
            queryWrapper.orderByAsc("order_status");
        };
        queryWrapper.eq("is_delete",1);
        List<Order> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Order> pageOrderTwo(Integer pageNum, Integer pageSize, int sort) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        0为创建时间 1为支付方式 2订单来源  3订单状态
        if(sort==0){
            queryWrapper.orderByAsc("create_time");
        };
        if (sort==1){
            queryWrapper.orderByAsc("pay_type");
        };
        if(sort==2){
            queryWrapper.orderByAsc("source_type");
        };
        if (sort==3){
            queryWrapper.orderByAsc("order_status");
        };
        queryWrapper.eq("is_delete",1).eq("order_status",2);
        List<Order> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<Order> pageSelect(Integer pageNum, Integer pageSize,int sort, Integer orderStatus, String transactionId, String receiverMobile, Date createTime,Integer sourceType) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        订单状态
if (orderStatus!=null){
        queryWrapper.eq("order_status",orderStatus);
};
//        交易流水号
if(transactionId!=null){
    queryWrapper.like("transaction_id",transactionId);
};
//        收货人手机
        if(receiverMobile!=null){
            queryWrapper.like("receiver_mobile",receiverMobile);
        };
        //        订单创建时间
        if(createTime!=null){
            queryWrapper.ge("create_time",createTime);
        };


        //        订单来源
        if(sourceType!=null){
            queryWrapper.eq("source_type",sourceType);
        };

//        排序


        //        0为创建时间 1为支付方式 2订单来源  3订单状态
        if(sort==0){
            queryWrapper.orderByAsc("create_time");
        };
        if (sort==1){
            queryWrapper.orderByAsc("pay_type");
        };
        if(sort==2){
            queryWrapper.orderByAsc("source_type");
        };
        if (sort==3){
            queryWrapper.orderByAsc("order_status");
        };
        queryWrapper.eq("is_delete",1);

        List<Order> list=this.list(queryWrapper);
        return list;
    };

    @Override
    public List<Order> pageSelectTwo(Integer pageNum, Integer pageSize, int sort, String transactionId, String receiverMobile, Date createTime, Integer sourceType) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
//        订单状态

            queryWrapper.eq("order_status",2);

//        交易流水号
        if(transactionId!=null){
            queryWrapper.like("transaction_id",transactionId);
        };
//        收货人手机
        if(receiverMobile!=null){
            queryWrapper.like("receiver_mobile",receiverMobile);
        };
        //        订单创建时间
        if(createTime!=null){
            queryWrapper.ge("create_time",createTime);
        };


        //        订单来源
        if(sourceType!=null){
            queryWrapper.eq("source_type",sourceType);
        };

//        排序


        //        0为创建时间 1为支付方式 2订单来源  3订单状态
        if(sort==0){
            queryWrapper.orderByAsc("create_time");
        };
        if (sort==1){
            queryWrapper.orderByAsc("pay_type");
        };
        if(sort==2){
            queryWrapper.orderByAsc("source_type");
        };
        if (sort==3){
            queryWrapper.orderByAsc("order_status");
        };
        queryWrapper.eq("is_delete",1);

        List<Order> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public int updateListClose(OrderRepVo vo) {

       List<Order> list=new ArrayList<>();
        for (Integer id:vo.getIds()
             ) {
//            查询
            Order order=this.getById(id);
            if(order.getOrderStatus()!=4){
                order.setOrderStatus(4);
            };
            list.add(order);
        };

//        批量修改
        boolean sucess=this.updateBatchById(list);

        return sucess?1:0;
    };

    @Override
    public int updateListSuccess(OrderRepVo vo) {
        List<Order> list=new ArrayList<>();
        for (Integer id:vo.getIds()
        ) {
//            查询
            Order order=this.getById(id);
            if(order.getOrderStatus()!=3){
                order.setOrderStatus(3);
            };
            list.add(order);
        };

//        批量修改
        boolean sucess=this.updateBatchById(list);

        return sucess?1:0;
    }

    @Override
    public int deleteList(OrderRepVo vo) {
        List<Order> list=new ArrayList<>();
        for (Integer id:vo.getIds()
        ) {
//            查询
            Order order=this.getById(id);
            if(order.getIsDelete()==1){
//                0删1现
       order.setIsDelete(0);
            };
            list.add(order);
        };

//        批量修改
        boolean sucess=this.updateBatchById(list);

        return sucess?1:0;
    };

    @Override
    public int getAllObjs(Integer orderStatus) {

        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        if(orderStatus!=null){
            wrapper.eq("order_status",orderStatus);
        }
        wrapper.eq("is_delete",1);
        int count=this.count(wrapper);
        return count;
    };

    @Override
    public int updateOne(OrderListReqVo vo) {
        boolean sucess=false;
        for (Order order: vo.getOrderList()
             ) {

            Order list=new Order();
            list.setId(order.getId());
            list.setShippingName(order.getShippingName());
            list.setShippingCode(order.getShippingCode());
            list.setOrderStatus(2);
            sucess=this.updateById(list);
        }
        return sucess?1:0;
    }
}
